<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Add a new user account.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.11) Fixes bug: Shows form twice while having invalid input.
 * 		 Fixes bug: Validates uselessly while using 'is_null' for non-input data,
 * 					changes to 'empty'.
 * 0.2)  Inherited from Addaction.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_addaction.php';
require_once INSTALLDIR . '/lib/slh_rights.php';

require_once INSTALLDIR . '/widgets/slh_form_addprofileuser.php';

require_once INSTALLDIR . '/classes/User.php';
require_once INSTALLDIR . '/classes/Profile.php';


class AddprofileuserAction extends AddAction
{
	function title()
	{
		return '添加社联会用户';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_PROFILE)) {
			return false;
		}
		
		return parent::prepare($args);
	}

	/**
	 * Shows the guidance.
	 * Overriding.
	 */
	function showGuidance()
	{
		$this->element('h3', null, '添加一个新的社联会用户');
		
		$this->instruction('呃,说明下添加的具体是什么东东,怎么添加好了.');
		
		// If added successfully, then?
		if ($this->added) {
			$this->element('p', 'cls_p_success', '添加成功!');
			// Or show a nav.
		}
	}
	
	/**
	 * Shows the widgets like form, or etc.
	 * Overriding.
	 */
	function showForm()
	{
		$form = new AddprofileuserForm($this, $this->args);
		$form->show();
	}
	
	/**
	 * Try to add the profile into the database.
	 */
	function tryAdd()
	{
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();
		
		$username	= $this->getTrimmedArg('username');
		$realname	= $this->getTrimmedArg('realname');
		$pinyin		= $this->getTrimmedArg('pinyin');
		$department	= $this->getTrimmedArg('department');
		
		// Rights.
		$right_news 		= $this->getTrimmedArg('right_news');
		$right_photoset		= $this->getTrimmedArg('right_photoset');
		$right_data			= $this->getTrimmedArg('right_data');
		$right_association	= $this->getTrimmedArg('right_association');
		$right_profile		= $this->getTrimmedArg('right_profile');
		
// TODO: Validates the data.
		if (empty($username)) {
			$this->showAction('请输入用户名.');
		} else if (empty($realname)) {
			$this->showAction('请输入真实姓名.');
		} else {
			// Rights handlers.
			$profile_right = 0;
			if ('true' == $right_news) {
				$profile_right = Rights::grantRight($profile_right, RIGHT_NEWS);
			}
			if ('true' == $right_photoset) {
				$profile_right = Rights::grantRight($profile_right, RIGHT_PHOTOSET);
			}
			if ('true' == $right_data) {
				$profile_right = Rights::grantRight($profile_right, RIGHT_DATA);
			}
			if ('true' == $right_association) {
				$profile_right = Rights::grantRight($profile_right, RIGHT_ASSOCITION);
			}
			if ('true' == $right_profile &&
				Rights::rootProtection()) {
				$profile_right = Rights::grantRight($profile_right, RIGHT_PROFILE);
			}
			
			// Adds into the database.
			$profile_id = Profile::add(array('username' => $username,
											 'realname' => $realname,
											 'pinyin' => $pinyin,
											 'department' => $department,
											 'profile_rights' => $profile_right));
			
			if ( !$profile_id) {
				$this->showAction('添加用户失败!');
				return;
			}
			
			// Success.
			// Unset the seesion token preventing post-again.
			$this->unsetSessionToken();
			
			// Unset the seted values.
			unset($this->args['username']);
			unset($this->args['realname']);
			unset($this->args['pinyin']);
			unset($this->args['department']);
			
			$this->added = true;
			$this->showAction();
		}
	}
}

?>